package logic

import (
	"context"
	"go-zero-all-demos/apps/order/models"
	"time"

	"go-zero-all-demos/apps/order/pb/order"
	"go-zero-all-demos/apps/order/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddOrderLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddOrderLogic {
	return &AddOrderLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AddOrder 创建订单
func (l *AddOrderLogic) AddOrder(in *order.OrderInfo) (*order.OrderID, error) {
	id := time.Now().Unix()
	err := l.svcCtx.OrderModel.Insert(l.ctx, l.svcCtx.DB, &models.Order{Id: id, UserId: in.UserId, OrderId: in.OrderId})
	if err != nil {
		return nil, err
	}
	return &order.OrderID{Id: id}, nil
}
